package cn.formal.service.impl;

import cn.formal.entity.PageResult;
import cn.formal.pojo.QuestionAnswer;
import cn.formal.repository.QuestionAnswerRepository;
import cn.formal.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class SearchServiceImpl implements SearchService {

    @Autowired
    private QuestionAnswerRepository questionAnswerRepository;

    @Override
    public void addDoc(QuestionAnswer questionAnswer) {
        //使用时间戳作为id
        Long id = questionAnswer.getId();
        if (id==null||id==0){
            questionAnswer.setId(System.currentTimeMillis());
        }
        questionAnswerRepository.save(questionAnswer);
    }

    @Override
    public QuestionAnswer findById(Long id) {
        Optional<QuestionAnswer> answerOptional = questionAnswerRepository.findById(id);
        if (answerOptional.isPresent()){
            QuestionAnswer questionAnswer = answerOptional.get();
            log.info("[findById-返回值] - questionAnswer: {}",questionAnswer);
            return questionAnswer;
        }else {
            throw new RuntimeException("没有这个问答");
        }
    }

    @Override
    public List<QuestionAnswer> findByTitle(String title) {
        MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("title", title);
        Iterable<QuestionAnswer> questionAnswers = questionAnswerRepository.search(queryBuilder);
        List<QuestionAnswer> answerList = new ArrayList<>();
        for (QuestionAnswer questionAnswer : questionAnswers) {
            answerList.add(questionAnswer);
        }
        log.info("[findByTitle-返回值] - answerList: {}",answerList);
        return answerList;
    }

    @Override
    public void update(QuestionAnswer questionAnswer) {
        log.info("[updateById-接收值] - questionAnswer: {}",questionAnswer);
        questionAnswerRepository.save(questionAnswer);
    }

    @Override
    public void deleteById(Long id) {
        questionAnswerRepository.deleteById(id);
    }

    @Override
    public void deleteAll(){
        questionAnswerRepository.deleteAll();
    }

    @Override
    public PageResult findAll(Integer page, Integer size) {
        MatchAllQueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
        //创建排序规则
        Sort sort = new Sort(Sort.Direction.ASC, "id");
        //创建分页规则
        PageRequest pageRequest = PageRequest.of(page, size,sort);
        Page<QuestionAnswer> answerPage = questionAnswerRepository.search(queryBuilder,pageRequest);
        //获取总页数
        int totalPages = answerPage.getTotalPages();
        //获取总条数
        long total = answerPage.getTotalElements();
        //获取分页后的数据
        List<QuestionAnswer> answerList = answerPage.getContent();
        log.info("[findAll-返回值] - totalPages: {} - total: {} - answerList: {}",totalPages,total,answerList);
        return new PageResult(totalPages,total,answerList);
    }
}
